<!-- 本示例未包含完整css，获取外链css请参考上文，在hello uni-app项目中查看 -->
<template>
	<view @click="toChooseCity">
		<view class="uni-input city"
			style="font-size: 28rpx;	overflow: hidden;text-overflow: ellipsis;white-space: nowrap;">
			<text v-if="cityData.regionName">{{ cityData.regionName  }}</text>
			<text v-else @click="locationImageClick">{{ lo[loKey] }}</text>
		</view>
	</view>
</template>


</view>
</template>
<script>
	import {
		getCityApi,
		getDefaultLoacation
	} from "@/api/api.js";

	import {
		getCityId,
		getLocation,
		getLocationII
	} from '@/filters/commFilter.js'


	export default {
		props: {
			bs: {
				type: String,
				default: 'd'
			}
		},
		data() {
			return {
				cityData: {},
				show: true,
				cityIndex: [0, 0, 0],
				cityTree: [],
				amapPlugin: {},
				QQMapWX: {},
				latitude: '',
				longitude: '',
				lo: {
					0: '定位中',
					1: '重新定位'
				},
				loKey: 1,
				notick: 0
			}
		},
		async mounted() {
			let that = this;
			// await this.getDefaultLoc();
			// this.getCityList();
			// uni.$off('chooseCity');
			// uni.$on("chooseCity", data => {


			// })
			this.locationImageClick();

			uni.$on('reloadCity', () => {
				console.log("reloadCity")
				this.locationImageClick();
			})
		},
		destroyed() {
			// uni.$off('chooseCity');
		},
		methods: {
			//定位
			locationImageClick() {
				let that = this;
				this.loKey = 0;
				let storageCity = uni.getStorageSync('city');
				let storageCurData = uni.getStorageSync('curCityData');
				// console.log("$curCityData", this.$curCityData)
				if (this.$curCityData) {
					this.cityData = JSON.parse(JSON.stringify(this.$curCityData))
				} else {
					storageCurData = storageCurData ? JSON.parse(storageCurData) : null;
					getCityId().then(city => {
						if (storageCurData && storageCurData.regionId != city.county[1]) {
							this.hhhh(city)
						} else {
							this.cityData.regionName = city.countyName[2]
							this.cityData.regionId = city.county[1]
							this.cityData = JSON.parse(JSON.stringify(this.cityData))
							that.$setCity(city.county[1]);
							that.$setCurCityData(this.cityData)
							this.$emit('change');
						}
						
					}).catch(e=>{
						this.getDefaultLoc();
					}).finally(() => {
						this.loKey = 1;
						// console.log("默认", that.cityData,!storageCurData)
						// if (!that.cityData.regionId && !storageCurData) {
						// 	this.getDefaultLoc();
						// }
					});
				}
			},
			hhhh(city) {
				let that = this;
				if (!city.cityId) { //如果没有城市 直接设置成默认城市
					return;
				}
				if (that.cityData.regionId == city.county[1]) { //如果相等不会提示
					return;
				}
				uni.showModal({
					title: '提示',
					cancelText: "不切换",
					confirmText: '切换',
					content: '是否切换回定位城市' + city.countyName[2],
					success: function(res) {
						if (res.confirm) {
							//切换回定位城市
							that.cityData.regionName = city.countyName[2]
							that.cityData.regionId = city.county[1]
							that.cityData = JSON.parse(JSON.stringify(that.cityData))
							that.$setCurCityData(that.cityData)
							that.$setCity(city.county[1]);
							that.$emit('change');
						}
					}
				});
			},
			getDefaultLoc(callback) {
				let that = this;
				if(this.regionId){
					return;
				}
				getDefaultLoacation().then(res => {
						
					if (res.code == 200) {
						let cid = res.data.city;
						let cname = res.data.cityName;
						that.cityData.regionName = cname
						that.cityData.regionId = cid
						that.cityData = JSON.parse(JSON.stringify(that.cityData))
						that.$setCurCityData(that.cityData)
						that.$setCity(cid);
						that.$emit('change');
					}
				})
			},
			toChooseCity() {
				let that = this;
				console.log("that.bs", that.bs)
				uni.navigateTo({
					url: "/pages/others/getCity/getCity?bs=" + that.bs,
					events: {
						changeSuccess(data) {
							console.log("changeSuccess", data)
							console.log(that.bs, data.bs)
							that.show = true;
							getLocationII(data.data.regionName).then(res => {
								const latitude = res.result.location.lat;
								const longitude = res.result.location.lng;
								//设置选择的定位
								that.$setChooseLocation({
									latitude,
									longitude
								})
								data.isReturn && data.isReturn(() => {
									console.log(that.cityData, data.data)
									that.cityData = data.data;
									if (that.bs != data.bs) {
										that.$emit('change');
										return;
									}
									that.$setCurCityData(data.data)
									that.$setCity(data.data.regionId)
									that.$emit('change');
								});

							})
						}
					}
				})
			},
			// handleData(cityId) {
			// 	this.cityTree.forEach((province, prIndex) => {
			// 		province.child.forEach((city, ciIndex) => {
			// 			/* city.child.forEach( ( county , countyIndex) => {
			// 				if( county.regionId == cityId ) {
			// 					this.cityData[1] = province.child;
			// 					this.cityData[2] = city.child;
			// 					this.cityIndex = [ prIndex,ciIndex,countyIndex ]
			// 					this.$emit('change', city)
			// 					return
			// 				} 
			// 			}) */
			// 			if (city.regionId == cityId) {
			// 				this.cityData[1] = province.child;
			// 				this.cityData[2] = city.child;
			// 				console.log('cityData', this.cityData)
			// 				this.cityIndex = [prIndex, ciIndex, 0]
			// 				this.$emit('change')
			// 				return
			// 			}

			// 		})
			// 	})
			// },
			// columnchange(e) {
			// 	const {
			// 		column,
			// 		value
			// 	} = e.detail ? e.detail : 0;
			// 	this.cityIndex[column] = value
			// 	//对数据进行处理
			// 	//省份发生变化
			// 	let city = column == 0 ? (this.cityTree[value].child ? this.cityTree[value].child : []) :
			// 		this.cityData[1]
			// 	//当省份或者市改变 区也会发生改变
			// 	let county = column == 0 ? (city[0].child) : (column == 1 ? city[value].child : this
			// 		.cityData[2])
			// 	this.cityData = [this.cityTree, city, county];
			// 	this.$emit('change');
			// },
			// //点击确定
			// bindChange(e) {
			// 	const {
			// 		value
			// 	} = e.detail;
			// 	this.$emit('change');
			// }
		}
	}
</script>
<style scoped lang="scss">
	.city {
		margin-right: 26rpx;
		padding-right: 30rpx;
		position: relative;
		font-size: 16px;
		font-weight: 400;
		color: rgba(0, 0, 0, 1);
		max-width: 140rpx;
		lines: 1;
		text-overflow: ellipsis;

		&::after {
			display: block;
			content: '';
			width: 26rpx;
			height: 16rpx;
			background-image: url('/static/images/city_sanjiao.png');
			background-size: 100% 100%;
			background-repeat: no-repeat;
			position: absolute;
			right: 0;
			top: 50%;
			margin-top: -8rpx;
		}
	}
</style>